package com.enqualcomm.kids.mvp.heartrate;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.enqualcomm.kids.R;
import com.enqualcomm.kids.network.socket.response.HeartRateQueryResult;

import java.util.List;

/**
 * Created by Sunzhigang on 2016/7/20.
 */
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

    Context context;
    List<HeartRateQueryResult.Result> resultList;

    public MyAdapter(Context context) {
        this.context = context;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder viewHolder = new MyViewHolder(LayoutInflater.from(context)
                .inflate(R.layout.heart_rate_item, parent, false));
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        final HeartRateQueryResult.Result heartRate = resultList.get(position);
        String type = "";
        if ("0".equals(heartRate.type)) {
            type = "周期";
            holder.type_tv.setTextColor(Color.parseColor("#777777"));
        } else if ("1".equals(heartRate.type)) {
            type = "主动";
            holder.type_tv.setTextColor(Color.parseColor("#4fd9be"));
        } else if ("2".equals(heartRate.type)) {
            type = "远程";
            holder.type_tv.setTextColor(Color.parseColor("#4fd9be"));
        } else if ("SOS".equals(heartRate.type)) {
            type = "SOS";
            holder.type_tv.setTextColor(Color.parseColor("#ff0000"));
        }

        float v = Float.parseFloat(heartRate.heartrate);
        if (v<60){
            holder.heart_rate_tv.setTextColor(Color.parseColor("#30ff00"));
        }else if (v>90){
            holder.heart_rate_tv.setTextColor(Color.parseColor("#ff0000"));
        }else {
            holder.heart_rate_tv.setTextColor(Color.parseColor("#777777"));
        }

        holder.type_tv.setText(type);
        holder.time_tv.setText(heartRate.time);
        holder.heart_rate_tv.setText(heartRate.heartrate);
    }

    @Override
    public int getItemCount() {
        return resultList == null ? 0 : resultList.size();
    }

    public void setData(List<HeartRateQueryResult.Result> resultList) {
        this.resultList = resultList;
        notifyDataSetChanged();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        public TextView heart_rate_tv;
        public TextView time_tv;
        public TextView type_tv;

        public MyViewHolder(View itemView) {
            super(itemView);
            heart_rate_tv = (TextView) itemView.findViewById(R.id.heart_rate_tv);
            time_tv = (TextView) itemView.findViewById(R.id.time_tv);
            type_tv = (TextView) itemView.findViewById(R.id.type_tv);
        }
    }
}
